<template>
  <t-modal
    :title="operateType == 1 ? '新增' : '编辑'"
    width="900px"
    :visible="visible"
    @cancel="closeModal()"
    @ok="handelOk()"
    wrapClassName="addFinalModal-container"
    v-bind="footer"
    forceRender
  >
    <div class="curve-modal-body">
      <a-form-model ref="ruleForm" :model="form" :rules="rules">
        <a-form-model-item label="编号" prop="nodeCode">
          <a-input v-model.trim="form.nodeCode" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="档号" prop="documentCode">
          <a-input v-model.trim="form.documentCode" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="类目" prop="nodeName">
          <a-input v-model.trim="form.nodeName" placeholder="请输入" />
        </a-form-model-item>
      </a-form-model>
    </div>
  </t-modal>
</template>

<script>
import {
  saveConfigTree,
  detailConfigTree,
  updateConfigTree,
} from "@/api/safetyManage";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    operateType: {
      type: [String, Number],
      default: "1",
    },
    parameter: {
      type: Object,
      default() {
        return {};
      },
    },
    footer: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        //编号
        nodeCode: "",
        //档号
        documentCode: "",
        //类目
        nodeName: "",
      },
      rules: {
        nodeCode: [
          {
            required: true,
            message: "请输入编号",
          },
        ],
        documentCode: [
          {
            required: true,
            message: "请输入档号",
          },
        ],
        nodeName: [
          {
            required: true,
            message: "请输入类目",
          },
        ],
      },
    };
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          let { uuid } = this.parameter;
          if (uuid) {
            this.getDetail();
          }
        }
      },
      immediate: true,
    },
  },

  methods: {
    saveConfigTree,
    updateConfigTree,
    getDetail() {
      let { uuid } = this.parameter;
      return new Promise((resolve, reject) => {
        detailConfigTree({ uuid })
          .then((res) => {
            let { data } = res;
            this.form = {
              //编号
              nodeCode: data.nodeCode,
              //档号
              documentCode: data.documentCode,
              //类目
              nodeName: data.nodeName,
            };
            resolve();
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {});
      });
    },
    handelOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { form } = this;
          let { proCode, mainUuid, pid, uuid } = this.parameter;
          let formJson = {
            //编号
            nodeCode: form.nodeCode,
            //档号
            documentCode: form.documentCode,
            //类目
            nodeName: form.nodeName,
          };
          let url = "";
          //编辑
          if (uuid) {
            formJson.uuid = uuid;
            url = "updateConfigTree";
          } else {
            formJson.proCode = proCode;
            formJson.mainUuid = mainUuid;
            formJson.pid = pid;
            url = "saveConfigTree";
          }
          this[url](formJson)
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                this.$message.success("操作成功", 1.5);
                this.closeModal();
                this.$emit("ok");
              } else {
                this.$message.error(res?.message ?? "操作失败");
              }
            })
            .catch((_) => {});
        }
      });
    },
    //重置表单
    resetForm() {
      this.form = {
        //编号
        nodeCode: "",
        //档号
        documentCode: "",
        //类目
        nodeName: "",
      };
      this.$refs.ruleForm.resetFields();
    },
    //关闭
    closeModal() {
      this.resetForm();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.addFinalModal-container {
  .curve-modal-body {
    max-height: 700px;
    overflow: auto;
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 100px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
}
</style>
